#ifndef DRAGDROP_H
#define DRAGDROP_H

#include <QObject>
#include <QPointF>

class QGraphicsSceneDragDropEvent;
class QGraphicsSceneMouseEvent;
class QGraphicsItem;
class QMouseEvent;
class QGraphicsObject;
class QDropEvent;

// process of drag & drop; @20150923;
// 1,mouse press on an item, record the start pos;
// 2,trace mouse move event on the item, if distance is engouth, then start a drag;
// 3,start drag: create a drag object;
// 4, other items will receive dragEnterEvent(), dragMoveEvent(),dropEvent();
// ,if them setAccepDrops(true);

// relationship of view/item drag-event;
// if view setAccepDrops(false), then item will not receive too, even it set true;
// view should not deal event, then item will receive;

enum MouseAction_e{
	eDefaultAction,
	eDrawRect,
	eDrawLine,
	eDragItem,
	eResizeItem
};
class DragDrop : public QObject
{
	Q_OBJECT

public:
	DragDrop(QObject *parent = 0);
	~DragDrop();

	int initDragDrop();

	void sceneDragMoveEvent(QGraphicsSceneDragDropEvent *event);

	// mousePressEvent sequence: 
	//1)view->scene->mainWindow;
	//2)view->scene->item;
	bool viewMousePressEvent(QMouseEvent * event);
	bool viewMouseReleaseEvent(QMouseEvent * event);
	bool viewMouseMoveEvent(QMouseEvent * event);
	void viewDropEvent(QDropEvent *event);

	bool itemMousePressEvent(QGraphicsSceneMouseEvent *event,QGraphicsItem * item);
	bool itemMouseMoveEvent(QGraphicsSceneMouseEvent *event,QGraphicsItem * item);
	void performDrag();
	bool itemDragEnterEvent(QGraphicsSceneDragDropEvent *event, QGraphicsItem * item);
	bool itemDragMoveEvent(QGraphicsSceneDragDropEvent * event, QGraphicsItem * item);
	bool itemDropEvent(QGraphicsSceneDragDropEvent * event, QGraphicsItem * item);

	QPointF getPressPos() { return mPressPos;}
	QPointF getReleasePos() { return mReleasePos; }
	QPointF getMovePos(){ return mMovePos; }
	QGraphicsItem * getPressItem() { return mPressedItem; }
	QGraphicsItem * getReleaseItem() { return mReleaseItem; }

	MouseAction_e getMouseAction(){ return mMouseAction; }
	void setMouseAction( MouseAction_e ma) { mMouseAction = ma; }

	int getResizeStatus() { return mResizeStatus; }
private:
	bool judgePressAction();

	int mResizeStatus;
	bool resizeItem();
	void judgeCursorInItem(QGraphicsItem * item, QPointF & p);

	bool isViewEvent();
	void doneMouseAction(QMouseEvent * event);
	bool judgeMouseMoveAction(QMouseEvent * event);
	QGraphicsItem * getItemAtPoint(QPointF & p);

	bool isOverDistance(QPointF & sp, QPointF & ep);
	void updateView();

	MouseAction_e mMouseAction;
	void resetStatus();
	void resetOnRelease();

	QGraphicsItem * mPressedItem;
	QGraphicsItem * mReleaseItem;

	QPointF mPressPos;
	QPointF mMovePos;
	QPointF mReleasePos;
};

extern DragDrop * gDragDrop;

#endif // DRAGDROP_H
